Imports System.Drawing
Imports System.Drawing.Imaging

Public Class CreateThumbnail
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Enum _thumbStyle
        ForceToLargest
        ForceToWidth
        ForceToHeight
    End Enum

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        Dim image As String = Request.QueryString("image")
        If image Is Nothing Then
            ErrorResult()
            Return
        End If

        'Get requested size.
        Dim size As Integer = 150
        If Not Request.QueryString("size") Is Nothing Then
            size = Int32.Parse(Request.QueryString("size"))
        End If

        'Determine if a particular axis should be forced.
        Dim forceStyle As _thumbStyle
        If Not Request.QueryString("force") Is Nothing Then

            Select Case Request.QueryString("force").ToLower

                Case "w"
                    forceStyle = _thumbStyle.ForceToWidth

                Case "h"
                    forceStyle = _thumbStyle.ForceToHeight

            End Select

        End If

        Dim path As String = Server.MapPath(Image)
        Dim bmp As Bitmap = CreateThumbnail(path, size, forceStyle)

        If bmp Is Nothing Then

            path = Server.MapPath(Request.ApplicationPath & "\Images\shim.gif")
            bmp = CreateThumbnail(path, size, forceStyle)

            If bmp Is Nothing Then
                ErrorResult()
                Return
            End If

        End If

        Response.ContentType = "image/jpeg"
        bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
        bmp.Dispose()

    End Sub

    Private Sub ErrorResult()
        Response.Clear()
        Response.StatusCode = 404
        Response.End()
    End Sub

    Private Function CreateThumbnail(ByVal lcFilename As String, ByVal size As Integer, ByVal forceStyle As _thumbStyle) As Bitmap

        'Check the file exists.
        If Not My.Computer.FileSystem.FileExists(lcFilename) Then
            Return Nothing
        End If

        Dim bmpOut As System.Drawing.Bitmap

        ' Try
        Dim loBMP As New Bitmap(lcFilename)
        Dim loFormat As ImageFormat = loBMP.RawFormat

        Dim lnRatio As Decimal
        Dim lnNewWidth As Integer = 0
        Dim lnNewHeight As Integer = 0

        Select Case forceStyle

            Case _thumbStyle.ForceToLargest

                'Don't thumbnail it if it's already small enough.
                If loBMP.Width < size And loBMP.Height < size Then
                    Return loBMP
                End If

                If loBMP.Width > loBMP.Height Then
                    lnRatio = Convert.ToDecimal(size / loBMP.Width)
                    lnNewWidth = size
                    Dim lnTemp As Decimal = loBMP.Height * lnRatio
                    lnNewHeight = Convert.ToInt32(lnTemp)
                Else
                    lnRatio = Convert.ToDecimal(size / loBMP.Height)
                    lnNewHeight = size
                    Dim lnTemp As Decimal = loBMP.Width * lnRatio
                    lnNewWidth = Convert.ToInt32(lnTemp)
                End If

            Case _thumbStyle.ForceToWidth

                If loBMP.Width < size Then
                    Return loBMP
                End If

                lnRatio = Convert.ToDecimal(size / loBMP.Width)
                lnNewWidth = size
                Dim lnTemp As Decimal = loBMP.Height * lnRatio
                lnNewHeight = Convert.ToInt32(lnTemp)

            Case _thumbStyle.ForceToLargest

                If loBMP.Height < size Then
                    Return loBMP
                End If

                lnRatio = Convert.ToDecimal(size / loBMP.Height)
                lnNewHeight = size
                Dim lnTemp As Decimal = loBMP.Width * lnRatio
                lnNewWidth = Convert.ToInt32(lnTemp)

        End Select

        bmpOut = New Bitmap(lnNewWidth, lnNewHeight)

        Dim g As Graphics = Graphics.FromImage(bmpOut)
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight)
        g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight)

        loBMP.Dispose()

        Return bmpOut

    End Function

End Class
